
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>创建你自己的 vuejs · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../JSXWTH.html" />
    
    
    <link rel="prev" href="../创建你自己的react/创建你自己的react.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    前端框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../创建你自己的react/创建你自己的react.html">
            
                <a href="../创建你自己的react/创建你自己的react.html">
            
                    
                    创建你自己的 react
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2" data-path="创建你自己的vuejs.html">
            
                <a href="创建你自己的vuejs.html">
            
                    
                    创建你自己的 vuejs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../JSXWTH.html">
            
                <a href="../JSXWTH.html">
            
                    
                    JSX 什么鬼(一起来写一个 JSX 渲染器)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../Redux:从0创建Store.html">
            
                <a href="../Redux:从0创建Store.html">
            
                    
                    Redux:从 0 创建 Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../我们来写个redux.html">
            
                <a href="../我们来写个redux.html">
            
                    
                    我们来写个 redux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >创建你自己的 vuejs</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <!-- @import "[TOC]" {cmd="toc" depthFrom=1 depthTo=6 orderedList=false} -->
<!-- code_chunk_output -->
<ul>
<li><a href="#1-&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs">1. &#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs</a></li>
<li><a href="#1-&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs">1. &#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs</a><ul>
<li><a href="#11-vuejs&#x6982;&#x89C8;">1.1 Vuejs&#x6982;&#x89C8;</a></li>
<li><a href="#12-vuejs&#x5185;&#x90E8;&#x7684;&#x7EC4;&#x4EF6;">1.2 Vuejs&#x5185;&#x90E8;&#x7684;&#x7EC4;&#x4EF6;</a><ul>
<li><a href="#121-&#x5B9E;&#x4F8B;&#x751F;&#x547D;&#x5468;&#x671F;">1.2.1 &#x5B9E;&#x4F8B;&#x751F;&#x547D;&#x5468;&#x671F;</a></li>
<li><a href="#122-&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;">1.2.2 &#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;</a></li>
<li><a href="#123-&#x865A;&#x62DF;dom">1.2.3 &#x865A;&#x62DF;DOM</a></li>
<li><a href="#124-&#x7F16;&#x8BD1;&#x5668;">1.2.4 &#x7F16;&#x8BD1;&#x5668;</a></li>
<li><a href="#125-&#x642D;&#x5EFA;&#x5F00;&#x53D1;&#x73AF;&#x5883;">1.2.5 &#x642D;&#x5EFA;&#x5F00;&#x53D1;&#x73AF;&#x5883;</a><ul>
<li><a href="#1251-&#x914D;&#x7F6E;rollup&#x6253;&#x5305;&#x6A21;&#x5757;">1.2.5.1 &#x914D;&#x7F6E;Rollup&#x6253;&#x5305;&#x6A21;&#x5757;</a></li>
<li><a href="#1252-&#x4E3A;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;karma&#x548C;jasmine">1.2.5.2 &#x4E3A;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;Karma&#x548C;Jasmine</a></li>
<li><a href="#1253-&#x76EE;&#x5F55;&#x7ED3;&#x6784;">1.2.5.3 &#x76EE;&#x5F55;&#x7ED3;&#x6784;</a></li>
</ul>
</li>
<li><a href="#126&#x542F;&#x52A8;">1.2.6&#x542F;&#x52A8;</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#2-&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;">2 &#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;</a><ul>
<li><a href="#21-dep">2.1 Dep</a></li>
<li><a href="#22-&#x57FA;&#x672C;&#x7684;observer">2.2 &#x57FA;&#x672C;&#x7684;Observer</a></li>
<li><a href="#23-&#x76D1;&#x63A7;&#x5D4C;&#x5957;&#x5BF9;&#x8C61;">2.3 &#x76D1;&#x63A7;&#x5D4C;&#x5957;&#x5BF9;&#x8C61;</a></li>
<li><a href="#24-&#x89C2;&#x6D4B;&#x8BBE;&#x7F6E;&#x6216;&#x5220;&#x9664;&#x6570;&#x636E;">2.4 &#x89C2;&#x6D4B;&#x8BBE;&#x7F6E;&#x6216;&#x5220;&#x9664;&#x6570;&#x636E;</a></li>
<li><a href="#25-&#x76D1;&#x63A7;&#x6570;&#x7EC4;">2.5 &#x76D1;&#x63A7;&#x6570;&#x7EC4;</a></li>
<li><a href="#26-watcher">2.6 Watcher</a></li>
<li><a href="#26-watcher">2.6 Watcher</a><!-- /code_chunk_output -->
<a href="https://github.com/jsrebuild/build-your-own-vuejs" target="_blank">&#x539F;&#x6587;&#x94FE;&#x63A5;</a></li>
</ul>
</li>
</ul>
<h1 id="1-&#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs">1. &#x521B;&#x5EFA;&#x4F60;&#x81EA;&#x5DF1;&#x7684;vuejs</h1>
<h2 id="11-vuejs&#x6982;&#x89C8;">1.1 Vuejs&#x6982;&#x89C8;</h2>
<p>vuejs&#x662F;&#x4E2A;&#x7B80;&#x5355;&#x800C;&#x5F3A;&#x5927;&#x7684;MVVM&#x5E93;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x642D;&#x5EFA;&#x73B0;&#x4EE3;web&#x7528;&#x6237;&#x754C;&#x9762;&#x3002;</p>
<p>&#x5199;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x65F6;&#xFF0C;Vue&#x5728;github&#x4E0A;&#x6709;36,312&#x4E2A;stars&#x3002;&#x5E76;&#x4E14;&#x6BCF;&#x6708;&#x6709;230,250&#x7684;npm&#x4E0B;&#x8F7D;&#x91CF;&#x3002;Vue2.0&#x66F4;&#x662F;&#x4E3A;&#x6E32;&#x67D3;&#x5C42;&#x5F15;&#x5165;&#x4E86;&#x8F7B;&#x91CF;&#x7684;&#x865A;&#x62DF;dom&#x5B9E;&#x73B0;&#xFF0C;&#x8FD9;&#x4E3A;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x548C;&#x539F;&#x751F;&#x7AEF;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x5E26;&#x6765;&#x4E86;&#x53EF;&#x80FD;</p>
<blockquote>
<p>&#x4F5C;&#x8005;&#x5199;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x7684;&#x65F6;&#x5019;&#xFF0C;Vue&#x5728;github&#x4E0A;&#x6709;36,312&#x4E2A;stars&#xFF0C;&#x800C;&#x8BD1;&#x8005;&#x7FFB;&#x8BD1;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x65F6;&#xFF0C;Vue&#x5DF2;&#x7ECF;&#x6709;107,032&#x4E2A;Stars,&#x5DF2;&#x7ECF;&#x8D85;&#x8FC7;&#x4E86;React&#x7684;106,047&#x4E2A;</p>
</blockquote>
<p>vuejs&#x5BA3;&#x79F0;&#x662F;&#x4E00;&#x79CD;&#x6E10;&#x8FDB;&#x5F0F;javascript&#x6846;&#x67B6;&#xFF0C;&#x5C3D;&#x7BA1;vuejs&#x7684;&#x6838;&#x5FC3;&#x5F88;&#x5C0F;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x914D;&#x5957;&#x4E86;&#x5F88;&#x591A;&#x76F8;&#x5E94;&#x7684;&#x5DE5;&#x5177;&#x548C;&#x5E93;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x5927;&#x578B;&#x5E94;&#x7528;&#x3002;</p>
<h2 id="12-vuejs&#x5185;&#x90E8;&#x7684;&#x7EC4;&#x4EF6;">1.2 Vuejs&#x5185;&#x90E8;&#x7684;&#x7EC4;&#x4EF6;</h2>
<p>&#x6211;&#x4EEC;&#x9996;&#x5148;&#x6765;&#x719F;&#x6089;&#x4E00;&#x4E0B;Vue&#x5185;&#x90E8;&#x7684;&#x6838;&#x5FC3;&#x7EC4;&#x4EF6;&#xFF0C;vue&#x7684;&#x5185;&#x90E8;&#x53EF;&#x4EE5;&#x62C6;&#x5206;&#x6210;&#x51E0;&#x90E8;&#x5206;</p>
<p><img src="imgs/vueinternals.png" alt="vueinternals"></p>
<h3 id="121-&#x5B9E;&#x4F8B;&#x751F;&#x547D;&#x5468;&#x671F;">1.2.1 &#x5B9E;&#x4F8B;&#x751F;&#x547D;&#x5468;&#x671F;</h3>
<p>&#x4E00;&#x4E2A;&#x65B0;&#x521B;&#x5EFA;&#x7684;Vue&#x5B9E;&#x4F8B;&#x4F1A;&#x7ECF;&#x5386;&#x591A;&#x4E2A;&#x9636;&#x6BB5;&#xFF0C;&#x6BD4;&#x5982;&#x89C2;&#x5BDF;&#x6570;&#x636E;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x4E8B;&#x4EF6;&#xFF0C;&#x7F16;&#x8BD1;&#x6A21;&#x677F;&#xFF0C;&#x6E32;&#x67D3;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x7279;&#x5B9A;&#x7684;&#x9636;&#x6BB5;&#x6CE8;&#x518C;&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#xFF0C;&#x8FD9;&#x4E9B;&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x5C06;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<h3 id="122-&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;">1.2.2 &#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;</h3>
<p>&#x6240;&#x8C13;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;&#x5C31;&#x662F;vue&#x2018;&#x6570;&#x636E;-&#x89C6;&#x56FE;&#x2019;&#x7ED1;&#x5B9A;&#x7684;&#x9ED1;&#x9B54;&#x6CD5;&#x6765;&#x6E90;&#x3002;&#x5F53;&#x4F60;&#x8BBE;&#x7F6E;vue&#x5B9E;&#x4F8B;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x89C6;&#x56FE;&#x76F8;&#x5E94;&#x7684;&#x66F4;&#x65B0;&#xFF0C;&#x53CD;&#x4E4B;&#x4EA6;&#x7136;&#x3002;</p>
<p>Vue&#x4F7F;&#x7528;<font color="red">Object.defineProperty</font>&#x4F7F;&#x6570;&#x636E;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&apos;&#x54CD;&#x5E94;&apos;&#x3002;&#x53E6;&#x5916;&#x4F7F;&#x7528;&#x4F17;&#x6240;&#x5468;&#x77E5;&#x7684;<strong>&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;</strong>&#x6765;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x53D8;&#x5316;&#x548C;&#x89C6;&#x56FE;&#x6E32;&#x67D3;</p>
<h3 id="123-&#x865A;&#x62DF;dom">1.2.3 &#x865A;&#x62DF;DOM</h3>
<p>&#x865A;&#x62DF;DOM&#x662F;&#x5728;&#x5185;&#x5B58;&#x91CC;&#x7684;javascript&#x5BF9;&#x8C61;&#x6811;&#xFF0C;&#x7528;&#x6765;&#x6620;&#x5C04;&#x771F;&#x5B9E;&#x7684;DOM&#x6811;&#x3002;</p>
<p>&#x5F53;&#x6570;&#x636E;&#x53D8;&#x5316;&#x4E86;&#xFF0C;vue&#x5C06;&#x4F1A;&#x6E32;&#x67D3;&#x4E00;&#x9897;&#x5168;&#x65B0;&#x7684;&#x865A;&#x62DF;DOM&#x6811;&#xFF0C;&#x5E76;&#x4FDD;&#x5B58;&#x8001;&#x7684;&#x6811;&#x3002;&#x865A;&#x62DF;DOM&#x6A21;&#x5757;&#x4F1A;diff&#x4E24;&#x9897;&#x6811;&#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x5E76;&#x628A;&#x53D8;&#x5316;&#x8865;&#x4E01;&#x5230;&#x771F;&#x5B9E;&#x7684;DOM&#x6811;&#x4E0A;&#x3002;</p>
<p>Vue&#x4F7F;&#x7528;<a href="https://github.com/snabbdom/snabbdom" target="_blank">snabbdom</a>&#x4F5C;&#x4E3A;&#x865A;&#x62DF;DOM&#x7684;&#x5B9E;&#x73B0;&#x57FA;&#x7840;&#xFF0C;&#x4E3A;&#x4E86;&#x548C;Vue&#x7684;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;&#x517C;&#x5BB9;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x57FA;&#x7840;&#x4E0A;&#xFF0C;&#x505A;&#x4E86;&#x4E9B;&#x7A0D;&#x5FAE;&#x7684;&#x4FEE;&#x6539;&#x3002;</p>
<h3 id="124-&#x7F16;&#x8BD1;&#x5668;">1.2.4 &#x7F16;&#x8BD1;&#x5668;</h3>
<p>&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x5DE5;&#x4F5C;&#x5C31;&#x662F;&#x628A;&#x6A21;&#x677F;&#x7F16;&#x8BD1;&#x6210;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF08;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811; ASTs&#xFF09;.&#x7F16;&#x8BD1;&#x5668;&#x628A;&#x6DF7;&#x6742;&#x7740;Vue&#x6307;&#x4EE4;(Vue&#x7684;&#x6307;&#x4EE4;&#x53EA;&#x662F;&#x4E9B;HTML&#x5C5E;&#x6027;)&#x7684;HTML&#x4EE5;&#x53CA;&#x5176;&#x4ED6;&#x5B9E;&#x4F53;&#x89E3;&#x6790;&#x6210;&#x4E00;&#x9897;&#x6811;&#xFF0C;&#x5E76;&#x4F1A;&#x6700;&#x5927;&#x5316;&#x68C0;&#x6D4B;&#x51FA;&#x6240;&#x6709;&#x7684;&#x9759;&#x6001;&#x5B50;&#x6811;&#xFF08;&#x6240;&#x6709;&#x6CA1;&#x6709;&#x52A8;&#x6001;&#x7ED1;&#x5B9A;&#x7684;&#x5B50;&#x6811;&#xFF09;&#xFF0C;&#x628A;&#x5B83;&#x4EEC;&#x79FB;&#x51FA;&#x6E32;&#x67D3;&#x3002;vue&#x4F7F;&#x7528;&#x7684;html&#x89E3;&#x6790;&#x5668;&#x662F;&#x7531;<a href="https://johnresig.com" target="_blank">John Resig</a>&#x6240;&#x5199;</p>
<blockquote>
<p>&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x5728;&#x8FD9;&#x91CC;&#x8BB2;&#x89E3;&#x7F16;&#x8BD1;&#x5668;&#x7684;&#x5B9E;&#x73B0;&#x7EC6;&#x8282;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x6784;&#x5EFA;&#x73AF;&#x8282;&#x4F7F;&#x7528;&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x628A;Vue&#x6A21;&#x677F;&#x90FD;&#x7F16;&#x8BD1;&#x6210;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x7F16;&#x8BD1;&#x5668;&#x5E76;&#x4E0D;&#x5305;&#x542B;&#x5728;Vue&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x4E2D;&#x3002;&#x53E6;&#x5916;&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5199;&#x6E32;&#x67D3;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x7F16;&#x8BD1;&#x5668;&#x5E76;&#x4E0D;&#x662F;&#x7406;&#x89E3;Vue&#x673A;&#x5236;&#x7684;&#x6838;&#x5FC3;&#x90E8;&#x5206;&#x3002;</p>
</blockquote>
<h3 id="125-&#x642D;&#x5EFA;&#x5F00;&#x53D1;&#x73AF;&#x5883;">1.2.5 &#x642D;&#x5EFA;&#x5F00;&#x53D1;&#x73AF;&#x5883;</h3>
<p>&#x521B;&#x5EFA;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;vuejs&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x8981;&#x505A;&#x4E00;&#x4E9B;&#x73AF;&#x5883;&#x914D;&#x7F6E;&#xFF0C;&#x5305;&#x62EC;&#x6A21;&#x5757;&#x6253;&#x5305;&#x5DE5;&#x5177;&#x4EE5;&#x53CA;&#x6D4B;&#x8BD5;&#x5DE5;&#x5177;&#x3002;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5C06;&#x91C7;&#x7528;<em>&#x6D4B;&#x8BD5;-&#x9A71;&#x52A8;</em>&#x7684;&#x6D41;&#x7A0B;</p>
<p>&#x56E0;&#x4E3A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;javascript&#x9879;&#x76EE;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x65B0;&#x9896;&#x7684;&#x5DE5;&#x5177;&#x3002;&#x9996;&#x5148;&#x5C31;&#x662F;&#x8DD1;&#x2018;npm init&#x2019;&#x547D;&#x4EE4;&#x5E76;&#x8BBE;&#x7F6E;&#x4E00;&#x4E9B;&#x9879;&#x76EE;&#x4FE1;&#x606F;</p>
<h4 id="1251-&#x914D;&#x7F6E;rollup&#x6253;&#x5305;&#x6A21;&#x5757;">1.2.5.1 &#x914D;&#x7F6E;Rollup&#x6253;&#x5305;&#x6A21;&#x5757;</h4>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;Rollup&#x6765;&#x6253;&#x5305;&#x6A21;&#x5757;&#x3002;<a href="https://rollupjs.org/guide/en" target="_blank">Rollup</a>&#x662F;&#x4E00;&#x4E2A;js&#x6A21;&#x5757;&#x6253;&#x5305;&#x5DE5;&#x5177;.&#x5B83;&#x5141;&#x8BB8;&#x4F60;&#x4E3A;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x6216;&#x5E93;&#x4F7F;&#x7528;ES6&#x7684;import/export&#x8BED;&#x6CD5;&#x6765;&#x8FDB;&#x884C;&#x6A21;&#x5757;&#x5316;&#x5F00;&#x53D1;&#x3002;vuejs&#x4E5F;&#x662F;&#x4F7F;&#x7528;Rollup&#x6765;&#x6253;&#x5305;&#x6A21;&#x5757;&#x7684;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4E3A;Rollup&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x3002;&#x5728;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x521B;&#x5EFA;rollup.conf.js&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  input: <span class="hljs-string">&apos;src/instance/index.js&apos;</span>,
  output: {
    name: <span class="hljs-string">&apos;Vue&apos;</span>,
    file: <span class="hljs-string">&apos;dist/vue.js&apos;</span>,
    format: <span class="hljs-string">&apos;iife&apos;</span>
  },
};
</code></pre>
<p>&#x4E0D;&#x8981;&#x5FD8;&#x4E86;&#x8DD1;&#x2018;npm install rollup rollup-watch --save-dev&#x2019;&#x547D;&#x4EE4;</p>
<h4 id="1252-&#x4E3A;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;karma&#x548C;jasmine">1.2.5.2 &#x4E3A;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;Karma&#x548C;Jasmine</h4>
<p>&#x6D4B;&#x8BD5;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x4E00;&#x4E9B;&#x5305;&#xFF0C;&#x8DD1;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code class="lang-shell">npm install karma jasmine karma-jasmine karma-chrome-launcher
 karma-rollup-plugin karma-rollup-preprocessor buble  rollup-plugin-buble --save-dev
</code></pre>
<p>&#x5728;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x521B;&#x5EFA;karma.conf.js&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">config</span>) </span>{
  config.set({
    files: [{ pattern: <span class="hljs-string">&apos;test/**/*.spec.js&apos;</span>, watched: <span class="hljs-literal">false</span> }],
    frameworks: [<span class="hljs-string">&apos;jasmine&apos;</span>],
    browsers: [<span class="hljs-string">&apos;Chrome&apos;</span>],
    preprocessors: {
      <span class="hljs-string">&apos;./test/**/*.js&apos;</span>: [<span class="hljs-string">&apos;rollup&apos;</span>]
    },
    rollupPreprocessor: {
      plugins: [
        <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;rollup-plugin-buble&apos;</span>)(),
      ],
      output: {
        format: <span class="hljs-string">&apos;iife&apos;</span>,
        name: <span class="hljs-string">&apos;Vue&apos;</span>,
        sourcemap: <span class="hljs-string">&apos;inline&apos;</span>
      }
    }
  })
}
</code></pre>
<h4 id="1253-&#x76EE;&#x5F55;&#x7ED3;&#x6784;">1.2.5.3 &#x76EE;&#x5F55;&#x7ED3;&#x6784;</h4>
<p>&#x6574;&#x4E2A;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-text">- package.json
- rollup.conf.js
- node_modules
- dist
- test
- src
  - observer
  - instance
  - util
  - vdom
</code></pre>
<h3 id="126&#x542F;&#x52A8;">1.2.6&#x542F;&#x52A8;</h3>
<p>&#x6211;&#x4EEC;&#x65B9;&#x4FBF;&#x8D77;&#x89C1;&#xFF0C;&#x5C06;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;npm&#x811A;&#x672C;
<em>package.json</em></p>
<pre><code class="lang-json"><span class="hljs-string">&quot;scripts&quot;</span>: {
   <span class="hljs-string">&quot;build&quot;</span>: <span class="hljs-string">&quot;rollup -c&quot;</span>,
   <span class="hljs-string">&quot;watch&quot;</span>: <span class="hljs-string">&quot;rollup -c -w&quot;</span>,
   <span class="hljs-string">&quot;test&quot;</span>: <span class="hljs-string">&quot;karma start&quot;</span>
}
</code></pre>
<p>&#x4E3A;&#x4E86;&#x542F;&#x52A8;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;vuejs,&#x5148;&#x5199;&#x6211;&#x4EEC;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;
<em>test/options/options.spec.js</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../../src/instance/index&quot;</span>;

describe(<span class="hljs-string">&apos;Proxy test&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    it(<span class="hljs-string">&apos;should proxy vm._data.a = vm.a&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue({
            data: {
                a: <span class="hljs-number">2</span>
            }
        })
        expect(vm.a).toEqual(<span class="hljs-number">2</span>);
    });
});
</code></pre>
<p>&#x8BE5;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x6D4B;&#x8BD5;vm&#x4E0A;data&#x7684;&#x5C5E;&#x6027;&#xFF08;&#x5982;vm._data.a&#xFF09;,&#x662F;&#x5426;&#x90FD;&#x4EE3;&#x7406;&#x5230;vm&#x4E0A;&#x4E86;&#xFF08;&#x6BD4;&#x5982;vm.a&#xFF09;&#x3002;&#x8FD9;&#x662F;vue&#x5176;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x5C0F;&#x6280;&#x5DE7;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E3A;&#x6211;&#x4EEC;&#x7684;vue&#x5199;&#x4E0B;&#x7B2C;&#x4E00;&#x884C;&#x771F;&#x6B63;&#x4EE3;&#x7801;
<em>src/instance/index.js</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { initMixin } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./init&apos;</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Vue</span> (<span class="hljs-params">options</span>) </span>{
  <span class="hljs-keyword">this</span>._init(options)
}

initMixin(Vue)

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Vue
</code></pre>
<p>&#x7C97;&#x770B;&#x53EA;&#x662F;Vue&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E86;this._init,&#x6CA1;&#x4EC0;&#x4E48;&#x7279;&#x522B;&#x7684;&#x5730;&#x65B9;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E0B;&#x2018;initMixin&#x2019;&#x5E72;&#x4E86;&#x4EC0;&#x4E48;&#xFF1A;
<em>src/instance/init.js</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { initState } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./state&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">initMixin</span> (<span class="hljs-params">Vue</span>) </span>{
  Vue.prototype._init = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">options</span>) </span>{
  <span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">this</span>
  vm.$options = options
  initState(vm)
  }
}
</code></pre>
<p>Vue&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x7528;<a href="http://oomusou.io/javascript/mixin/" target="_blank">&#x7EC7;&#x5165;&#x6A21;&#x5F0F;</a>&#x6765;&#x6CE8;&#x5165;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x5728;&#x540E;&#x9762;&#x53D1;&#x73B0;Vue&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x7EC7;&#x5165;&#x6A21;&#x5F0F;&#x6765;&#x6DFB;&#x52A0;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x3002;Minxin&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;&#x5230;&#x8BE5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x4E0A;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x6240;&#x4EE5;&apos;initMixin&#x2018;&#x6DFB;&#x52A0;&#x4E86;&apos;_init&apos;&#x65B9;&#x6CD5;&#x5230;&apos;Vue.prototype&apos;&#x3002;&#x800C;_init&#x65B9;&#x6CD5;&#x8C03;&#x7528;state.js&#x4E0B;&#x7684;initState&#x65B9;&#x6CD5;
<em>src/instance/state.js</em></p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">initState</span>(<span class="hljs-params">vm</span>) </span>{
  initData(vm)
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">initData</span>(<span class="hljs-params">vm</span>) </span>{
  <span class="hljs-keyword">var</span> data = vm.$options.data
  vm._data = data
  <span class="hljs-comment">// proxy data on instance</span>
  <span class="hljs-keyword">var</span> keys = <span class="hljs-built_in">Object</span>.keys(data)

  <span class="hljs-keyword">var</span> i = keys.length
  <span class="hljs-keyword">while</span> (i--) {
    proxy(vm, keys[i])
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">proxy</span>(<span class="hljs-params">vm, key</span>) </span>{
    <span class="hljs-built_in">Object</span>.defineProperty(vm, key, {
      configurable: <span class="hljs-literal">true</span>,
      enumerable: <span class="hljs-literal">true</span>,
      get: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">proxyGetter</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> vm._data[key]
      },
      set: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">proxySetter</span>(<span class="hljs-params">val</span>) </span>{
        vm._data[key] = val
      }
    })
}
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x7EC8;&#x4E8E;&#x770B;&#x5230;&#x4E86;proxy-&#x4EE3;&#x7406;&#x3002;initState&#x8C03;&#x7528;initData&#x65B9;&#x6CD5;,initData&#x904D;&#x5386;vm._data&#x4E0A;&#x7684;&#x6240;&#x6709;key,&#x5728;&#x6BCF;&#x4E2A;value&#x4E0A;&#x8C03;&#x7528;proxy&#x65B9;&#x6CD5;&#x3002;</p>
<p>proxy&#x7528;&#x540C;&#x6837;&#x7684;key&#x5728;vm&#x4E0A;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x6709;setter&#x548C;getter&#x65B9;&#x6CD5;&#xFF0C;&#x800C;getter&#x548C;setter&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x4ECE;vm._data&#x4E0A;&#x83B7;&#x53D6;&#x548C;&#x8BBE;&#x7F6E;&#x6570;&#x636E;&#x3002;</p>
<p>&#x8FD9;&#x5C31;&#x662F;vm.a&#x600E;&#x4E48;&#x4EE3;&#x7406;&#x5230;vm._data.a&#x7684;&#x3002;</p>
<p>&#x8DD1;&#x2018;npm run build&#x2019; &#x548C; &#x2018;npm run test&#x2019;,&#x4F60;&#x5E94;&#x8BE5;&#x4F1A;&#x770B;&#x5230;&#x4E0B;&#x9762;&#x7684;&#x7ED3;&#x679C;&#xFF1A;
<img src="imgs/1-result.png" alt="1-result"></p>
<p>&#x5F88;&#x68D2;&#xFF01;&#x4F60;&#x73B0;&#x5728;&#x5DF2;&#x7ECF;&#x542F;&#x52A8;&#x4E86;&#x81EA;&#x5DF1;&#x7684;vuejs&#xFF0C;&#x7EE7;&#x7EED;&#x52AA;&#x529B;&#xFF01;</p>
<h1 id="2-&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;">2 &#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;</h1>
<p>Vue&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;&#x4F7F;model&#x548C;view&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x636E;&#x7ED1;&#x5B9A;&#x663E;&#x5F97;&#x7B80;&#x5355;&#x81EA;&#x7136;&#x3002;&#x6570;&#x636E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;javascript&#x5BF9;&#x8C61;&#xFF0C;&#x5F53;data&#x53D8;&#x66F4;&#xFF0C;&#x89C6;&#x56FE;&#x5C31;&#x6839;&#x636E;&#x6700;&#x540E;&#x7684;&#x72B6;&#x6001;&#x76F8;&#x5E94;&#x5730;&#x66F4;&#x65B0;&#x3002;&#x582A;&#x79F0;&#x5B8C;&#x7F8E;&#x3002;
&#x5728;&#x5185;&#x90E8;&#xFF0C;vuejs&#x5C06;&#x904D;&#x5386;data&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x5E76;&#x628A;&#x5B83;&#x4EEC;&#x7528;Object.defineProperty&#x8F6C;&#x6210; getter/setter&#x65B9;&#x6CD5;
data&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x539F;&#x59CB;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x90FD;&#x5206;&#x914D;&#x4E00;&#x4E2A;Observer&#x5B9E;&#x4F8B;&#xFF0C;Observer&#x4F1A;&#x5148;&#x901A;&#x77E5;watchers&#x90FD;&#x662F;&#x8C01;&#x8BA2;&#x9605;&#x4E86;&#x8FD9;&#x4E9B;&#x503C;&#x7684;&#x53D8;&#x5316;&#x4E8B;&#x4EF6;&#x3002;
&#x6BCF;&#x4E00;&#x4E2A;<strong>Vue</strong>&#x5B9E;&#x4F8B;&#x90FD;&#x6709;&#x4E00;&#x4E2A;<strong>Watcher</strong>&#x5B9E;&#x4F8B;&#xFF0C;&#x5728;&#x7EC4;&#x4EF6;&#x4F5C;&#x4E3A;&#x4F9D;&#x8D56;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#x6765;&#x6536;&#x96C6;&#x6240;&#x6709;&apos;&#x88AB;&#x89E6;&#x78B0;&#x8FC7;&apos;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x5F53;&#x6570;&#x636E;&#x53D8;&#x5316;&#x4E86;&#x4E4B;&#x540E;&#xFF0C;watcher&#x4F1A;&#x91CD;&#x65B0;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#xFF0C;&#x5E76;&#x8DD1;&#x90A3;&#x4E9B;&#x5728;&#x521D;&#x59CB;&#x5316;watcher&#x7684;&#x65F6;&#x5019;&#x4F20;&#x8FC7;&#x6765;&#x7684;&#x56DE;&#x8C03;&#x3002;
&#x90A3;&#x4E48;&#xFF0C;&#x5982;&#x4F55;&#x901A;&#x77E5;watcher&#x6570;&#x636E;&#x53D8;&#x5316;&#x4E86;&#x5462;&#xFF1F;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x6765;&#x4E86;&#xFF01;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7C7B;&#x53EB;<strong>Dep</strong>&#x3002;&#x4F5C;&#x4E3A;&#x4E2D;&#x4ECB;&#x8005;&#xFF0C;&#x5B83;&#x7684;&#x610F;&#x601D;&#x5C31;&#x662F;&#x201C;&#x4F9D;&#x8D56;&#x201D;&#x3002;Oberserver&#x5B9E;&#x4F8B;&#x6709;&#x5BF9;&#x6240;&#x6709;&#x5F53;&#x6570;&#x636E;&#x53D8;&#x52A8;&#x5B83;&#x9700;&#x8981;&#x53BB;&#x901A;&#x77E5;&#x7684;deps&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x800C;&#x6BCF;&#x4E2A;dep&#x5B9E;&#x4F8B;&#x77E5;&#x9053;&#x54EA;&#x4E2A;watcher&#x9700;&#x8981;&#x53BB;&#x66F4;&#x65B0;&#x3002;
&#x5982;&#x679C;&#x4ECE;&#x4E0A;&#x5C42;&#x770B;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;&#x8FD0;&#x884C;&#x7684;&#x673A;&#x5236;&#x3002;&#x4E0B;&#x4E00;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x8BE6;&#x7EC6;&#x770B;&#x4E0B;&#x8FD9;&#x4E2A;&#x54CD;&#x5E94;&#x7CFB;&#x7EDF;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x7EC6;&#x8282;&#x3002;</p>
<h2 id="21-dep">2.1 Dep</h2>
<p>Dep&#x7684;&#x5B9E;&#x73B0;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x6BCF;&#x4E2A;dep&#x5B9E;&#x4F8B;&#x6709;&#x4E2A;uid&#x6765;&#x6807;&#x8BC6;&#x3002;subs&#x6570;&#x7EC4;&#x8BB0;&#x5F55;&#x4E86;&#x6240;&#x6709;&#x8BA2;&#x9605;&#x4E86;&#x8FD9;&#x4E2A;dep&#x5B9E;&#x4F8B;&#x7684;watcher. Dep.prototype.notify &#x8C03;&#x7814;subs&#x6570;&#x7EC4;&#x4E2D;&#x6BCF;&#x4E2A;&#x8BA2;&#x9605;&#x8005;&#x7684;&#x66F4;&#x65B0;&#x65B9;&#x6CD5;&#x3002;Dep.prototype.depend&#x662F;&#x5728;watcher&#x7684;&#x91CD;&#x65B0;&#x68C0;&#x67E5;&#x7684;&#x65F6;&#x5019;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x3002;&#x6211;&#x4EEC;&#x4E00;&#x4F1A;&#x518D;&#x8BB2;watcers&#x3002;&#x4F60;&#x73B0;&#x5728;&#x53EA;&#x9700;&#x8981;&#x77E5;&#x9053;Dep.target&#x5C31;&#x662F;&#x5728;&#x5F53;&#x65F6;&#x8981;&#x91CD;&#x65B0;&#x68C0;&#x67E5;&#x7684;watcher&#x5B9E;&#x4F8B;&#x3002;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x662F;&#x9759;&#x6001;&#x7684;&#xFF0C;&#x6240;&#x4EE5;Dep.target&#x662F;&#x5168;&#x5C40;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x4E00;&#x6B21;&#x53EA;&#x6307;&#x5411;&#x4E00;&#x4E2A;watcher&#x3002;</p>
<p>src/observer/dep.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span>  uid = <span class="hljs-number">0</span>

<span class="hljs-comment">// Dep contructor</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Dep</span>(<span class="hljs-params">argument</span>) </span>{
  <span class="hljs-keyword">this</span>.id = uid++
  <span class="hljs-keyword">this</span>.subs = []
}

Dep.prototype.addSub = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">sub</span>) </span>{
  <span class="hljs-keyword">this</span>.subs.push(sub)
}

Dep.prototype.removeSub = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">sub</span>) </span>{
  remove(<span class="hljs-keyword">this</span>.subs, sub)
}

Dep.prototype.depend = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> (Dep.target) {
    Dep.target.addDep(<span class="hljs-keyword">this</span>)
  }
}

Dep.prototype.notify = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> subs = <span class="hljs-keyword">this</span>.subs.slice()
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, l = subs.length; i &lt; l; i++) {
    subs[i].update()
  }
}

Dep.target = <span class="hljs-literal">null</span>
</code></pre>
<h2 id="22-&#x57FA;&#x672C;&#x7684;observer">2.2 &#x57FA;&#x672C;&#x7684;Observer</h2>
<p>&#x6211;&#x4EEC;&#x5148;&#x6765;&#x4E2A;&#x6A21;&#x677F;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// Observer constructor</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Observer</span>(<span class="hljs-params">value</span>) </span>{

}

<span class="hljs-comment">// API for observe value</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">observe</span> (<span class="hljs-params">value</span>)</span>{

}
</code></pre>
<p>&#x5B9E;&#x73B0;Oberver&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x5199;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;</p>
<p>test/observer/observer.spec.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  Observer,
  observe
} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../../src/observer/index&quot;</span>
<span class="hljs-keyword">import</span> Dep <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../../src/observer/dep&apos;</span>

describe(<span class="hljs-string">&apos;Observer test&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  it(<span class="hljs-string">&apos;observing object prop change&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> obj = { a:<span class="hljs-number">1</span>, b:{a:<span class="hljs-number">1</span>}, c:<span class="hljs-literal">NaN</span>}
    observe(obj)
    <span class="hljs-comment">// mock a watcher!</span>
    <span class="hljs-keyword">const</span> watcher = {
      deps: [],
      addDep (dep) {
        <span class="hljs-keyword">this</span>.deps.push(dep)
        dep.addSub(<span class="hljs-keyword">this</span>)
      },
      update: jasmine.createSpy()
    }
    <span class="hljs-comment">// observing primitive value</span>
    Dep.target = watcher
    obj.a
    Dep.target = <span class="hljs-literal">null</span>
    expect(watcher.deps.length).toBe(<span class="hljs-number">1</span>) <span class="hljs-comment">// obj.a</span>
    obj.a = <span class="hljs-number">3</span>
    expect(watcher.update.calls.count()).toBe(<span class="hljs-number">1</span>)
    watcher.deps = []
  });

});
</code></pre>
<p>&#x6211;&#x4EEC;&#x5148;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;js&#x5BF9;&#x8C61;obj&#x6765;&#x6A21;&#x62DF;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x7528;oberseve&#x65B9;&#x6CD5;&#x4F7F;&#x6570;&#x636E;&#x54CD;&#x5E94;&#x5F0F;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x8FD8;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;watcher&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6A21;&#x62DF;&#x4E00;&#x4E2A;watcer&#x3002;&#x4E00;&#x4E2A;watcer&#x6709;&#x4E00;&#x4E2A;&#x8BA2;&#x9605;&#x4F9D;&#x8D56;&#x7684;deps&#x6570;&#x7EC4;&#xFF0C;&#x5F53;&#x6570;&#x636E;&#x53D8;&#x5316;&#x65F6;&#x5019;update&#x65B9;&#x6CD5;&#x5C06;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x540E;&#x9762;&#x518D;&#x770B;addDep&#x65B9;&#x6CD5;&#x3002;
&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;jasmine&#x7684;&#x76D1;&#x63A7;&#x65B9;&#x6CD5;&#x5360;&#x4F4D;&#x3002;&#x4E00;&#x4E2A;&#x76D1;&#x63A7;&#x65B9;&#x6CD5;&#x4E0D;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#x3002;&#x5B83;&#x53EA;&#x8BB0;&#x5F55;&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x6B21;&#x6570;&#x548C;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;&#x3002;
&#x7136;&#x540E;&#x6211;&#x4EEC;&#x628A;&#x5168;&#x5C40;&#x7684;Dep.target&#x6307;&#x5411;watcher,&#x7136;&#x540E;&#x8BBE;&#x7F6E;obj.a&#x3002;&#x5982;&#x679C;&#x6570;&#x636E;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#xFF0C;watcher&#x7684;&#x66F4;&#x65B0;&#x65B9;&#x6CD5;&#x5C06;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;
&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5148;&#x4E13;&#x6CE8;&#x4E8E;observe&#x65B9;&#x6CD5;&#x3002;&#x4E0B;&#x9762;&#x5C31;&#x662F;&#x4EE3;&#x7801;&#x3002;observe&#x65B9;&#x6CD5;&#x5148;&#x68C0;&#x67E5;&#x503C;&#x662F;&#x4E0D;&#x662F;&#x5BF9;&#x8C61;&#x3002;&#x5982;&#x679C;&#x662F;&#x7684;&#x8BDD;&#xFF0C;&#x901A;&#x8FC7;&#x68C0;&#x67E5;&#x5B83;&#x7684;<strong>ob</strong>&#x5C5E;&#x6027;&#xFF0C;&#x68C0;&#x67E5;&#x8BE5;&#x503C;&#x662F;&#x4E0D;&#x662F;&#x5DF2;&#x7ECF;&#x6302;&#x4E86;&#x4E00;&#x4E2A;Observer&#x5B9E;&#x4F8B;&#x3002;
&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;Observer&#x5B9E;&#x4F8B;&#xFF0C;observe&#x5C31;&#x4F1A;&#x7ED9;&#x8BE5;&#x503C;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;Observer&#x5B9E;&#x4F8B;&#x5E76;&#x8FD4;&#x56DE;&#x3002;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  hasOwn,
  isObject
}
<span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../util/index&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">observe</span> (<span class="hljs-params">value</span>)</span>{
  <span class="hljs-keyword">if</span> (!isObject(value)) {
    <span class="hljs-keyword">return</span>
  }
  <span class="hljs-keyword">var</span> ob
  <span class="hljs-keyword">if</span> (hasOwn(value, <span class="hljs-string">&apos;__ob__&apos;</span>) &amp;&amp; value.__ob__ <span class="hljs-keyword">instanceof</span> Observer) {
    ob = value.__ob__
  } <span class="hljs-keyword">else</span> {
    ob = <span class="hljs-keyword">new</span> Observer(value)
  }
  <span class="hljs-keyword">return</span> ob
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5C0F;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;hasOwn&#xFF0C;&#x5B83;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x5BF9;Object.prototype.hasOwnProperty&#x7684;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x5C01;&#x88C5;:</p>
<p>src/util/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> hasOwnProperty = <span class="hljs-built_in">Object</span>.prototype.hasOwnProperty
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hasOwn</span> (<span class="hljs-params">obj, key</span>) </span>{
  <span class="hljs-keyword">return</span> hasOwnProperty.call(obj, key)
}
</code></pre>
<p>&#x8FD8;&#x6709;&#x53E6;&#x4E00;&#x4E2A;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;isObject&#xFF1A;</p>
<p>src/util/index.js</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isObject</span> (<span class="hljs-params">obj</span>) </span>{
  <span class="hljs-keyword">return</span> obj !== <span class="hljs-literal">null</span> &amp;&amp; <span class="hljs-keyword">typeof</span> obj === <span class="hljs-string">&apos;object&apos;</span>
}
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E0B;Observer&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;&#x5B83;&#x4F1A;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;Dep&#x5B9E;&#x4F8B;&#xFF0C;&#x4F20;&#x5165;&#x503C;&#x8C03;&#x7528;walk&#x65B9;&#x6CD5;&#x3002;&#x5E76;&#x4F1A;&#x628A;observer&#x4EE5;<strong>ob</strong>&#x7684;&#x5C5E;&#x6027;&#x6302;&#x5728;&#x503C;&#x4E0A;&#x3002;
src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  def, <span class="hljs-comment">//new</span>
  hasOwn,
  isObject
}
<span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../util/index&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Observer</span>(<span class="hljs-params">value</span>) </span>{
  <span class="hljs-keyword">this</span>.value = value
  <span class="hljs-keyword">this</span>.dep = <span class="hljs-keyword">new</span> Dep()
  <span class="hljs-keyword">this</span>.walk(value)
  def(value, <span class="hljs-string">&apos;__ob__&apos;</span>, <span class="hljs-keyword">this</span>)
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;def&#x662F;&#x4E2A;&#x65B0;&#x7684;&#x5DE5;&#x5177;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x4F1A;&#x7528;Object.defineProperty() API&#x6765;&#x7ED9;&#x5BF9;&#x8C61;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;
src/util/index.js</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">def</span> (<span class="hljs-params">obj, key, val, enumerable</span>) </span>{
  <span class="hljs-built_in">Object</span>.defineProperty(obj, key, {
    value: val,
    enumerable: !!enumerable,
    writable: <span class="hljs-literal">true</span>,
    configurable: <span class="hljs-literal">true</span>
  })
}
</code></pre>
<p>walk&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x904D;&#x5386;&#x5BF9;&#x8C61;&#xFF0C;&#x5BF9;&#x6BCF;&#x4E2A;&#x503C;&#x8C03;&#x7528;defineReactive&#x65B9;&#x6CD5;&#x3002;
src/observer/index.js</p>
<pre><code class="lang-js">Observer.prototype.walk = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) </span>{
  <span class="hljs-keyword">var</span> keys = <span class="hljs-built_in">Object</span>.keys(obj)
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; keys.length; i++) {
      defineReactive(obj, keys[i], obj[keys[i]])
  }
}
</code></pre>
<p>defineReactive&#x91CC;&#x5C31;&#x8981;&#x7528;&#x5230;Object.defineProperty&#x4E86;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">defineReactive</span> (<span class="hljs-params">obj, key, val</span>) </span>{
  <span class="hljs-keyword">var</span> dep = <span class="hljs-keyword">new</span> Dep()
  <span class="hljs-built_in">Object</span>.defineProperty(obj, key, {
    enumerable: <span class="hljs-literal">true</span>,
    configurable: <span class="hljs-literal">true</span>,
    get: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reactiveGetter</span> (<span class="hljs-params"></span>) </span>{
      <span class="hljs-keyword">var</span> value = val
      <span class="hljs-keyword">if</span> (Dep.target) {
        dep.depend()
      }
      <span class="hljs-keyword">return</span> value
    },
    set: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reactiveSetter</span> (<span class="hljs-params">newVal</span>) </span>{
      <span class="hljs-keyword">var</span> value =  val
      <span class="hljs-keyword">if</span> (newVal === value || (newVal !== newVal &amp;&amp; value !== value)) {
        <span class="hljs-keyword">return</span>
      }
      val = newVal
      dep.notify()
    }
  })
}
</code></pre>
<p>reactiveGetter&#x65B9;&#x6CD5;&#x68C0;&#x67E5;Dep.target&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#xFF0C;&#x8868;&#x793A;getter&#x65B9;&#x6CD5;&#x5728;watcher&#x624B;&#x673A;&#x4F9D;&#x8D56;&#x7684;&#x65F6;&#x5019;&#x88AB;&#x89E6;&#x53D1;&#x4E86;&#x3002;&#x5F53;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x8C03;&#x7528;dep.depend()&#x6765;&#x6DFB;&#x52A0;&#x4F9D;&#x8D56;&#x3002;dep.depend&#x65B9;&#x6CD5;&#x5B9E;&#x9645;&#x4E0A;&#x8C03;&#x7528;&#x7684;&#x662F;Dep.target.addDep(dep)&#x3002;&#x56E0;&#x4E3A;Dep.target&#x5C31;&#x662F;&#x4E00;&#x4E2A;watcher&#xFF0C;Dep.target.addDep(dep)&#x5C31;&#x7B49;&#x540C;&#x4E8E;watcher.addDep(dep),&#x6211;&#x4EEC;&#x770B;&#x770B;addDep&#x505A;&#x4E86;&#x4EC0;&#x4E48;&#xFF1A;</p>
<pre><code class="lang-js">addDep (dep) {
   <span class="hljs-keyword">this</span>.deps.push(dep)
   dep.addSub(<span class="hljs-keyword">this</span>)
}
</code></pre>
<p>&#x5B83;&#x628A;dep&#x653E;&#x8FDB;watcher&#x7684;deps&#x6570;&#x7EC4;&#x3002;&#x540C;&#x65F6;&#x628A;&#x76EE;&#x6807;watcher&#x653E;&#x5165;dep&#x7684;subs&#x6570;&#x7EC4;&#x3002;&#x6240;&#x4EE5;&#x8FD9;&#x5C31;&#x662F;&#x4F9D;&#x8D56;&#x5982;&#x4F55;&#x8FFD;&#x8E2A;&#x7684;&#x3002;</p>
<p>reactiveSetter&#x65B9;&#x6CD5;&#x4EC5;&#x4EC5;&#x5728;&#x65B0;&#x503C;&#x548C;&#x65E7;&#x503C;&#x4E0D;&#x540C;&#x7684;&#x65F6;&#x5019;&#x8BBE;&#x7F6E;&#x6210;&#x65B0;&#x503C;&#x3002;&#x540C;&#x65F6;&#x901A;&#x8FC7;dep.notify()&#x65B9;&#x6CD5;&#x6765;&#x901A;&#x77E5;watcher&#x6765;&#x66F4;&#x65B0;&#x3002;&#x6211;&#x4EEC;&#x56DE;&#x987E;&#x4E0B;&#x4E4B;&#x524D;Dep&#x90A3;&#x8282;&#xFF1A;</p>
<pre><code class="lang-js">Dep.prototype.notify = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> subs = <span class="hljs-keyword">this</span>.subs.slice()
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, l = subs.length; i &lt; l; i++) {
    subs[i].update()
  }
}
</code></pre>
<p>Dep.prototype.notify&#x8C03;&#x7528;subs&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6BCF;&#x4E2A;watcher&#x7684;update&#x65B9;&#x6CD5;&#x3002;&#x5BF9;&#xFF0C;&#x8FD9;&#x4E9B;watchers&#x5C31;&#x662F;&#x5728;Dep.target.addDep(dep)&#x7684;&#x65F6;&#x5019;&#x88AB;&#x653E;&#x8FDB;subs&#x6570;&#x7EC4;&#x7684;&#x3002;&#x73B0;&#x5728;&#x90FD;&#x8FDE;&#x8D77;&#x6765;&#x4E86;&#x3002;
&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x8DD1;&#x4E00;&#x4E0B;npm run test&#x547D;&#x4EE4;&#xFF0C;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x5199;&#x7684;&#x7528;&#x4F8B;&#x5E94;&#x8BE5;&#x901A;&#x8FC7;&#x4E86;&#x3002;</p>
<h2 id="23-&#x76D1;&#x63A7;&#x5D4C;&#x5957;&#x5BF9;&#x8C61;">2.3 &#x76D1;&#x63A7;&#x5D4C;&#x5957;&#x5BF9;&#x8C61;</h2>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x53EA;&#x80FD;&#x76D1;&#x63A7;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x7684;&#x7B80;&#x5355;&#x5BF9;&#x8C61;&#x3002;&#x6240;&#x4EE5;&#x8FD9;&#x4E00;&#x8282;&#x6211;&#x4EEC;&#x5C06;&#x6DFB;&#x52A0;&#x975E;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#xFF0C;&#x6BD4;&#x5982;&#x5BF9;&#x8C61;&#x7684;&#x76D1;&#x63A7;&#x652F;&#x6301;&#x3002;</p>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x628A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;:</p>
<p>test/observer/observer.spec.js</p>
<pre><code class="lang-js">  it(<span class="hljs-string">&apos;observing object prop change&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  &#xB7;&#xB7;&#xB7;
    <span class="hljs-comment">// observing non-primitive value</span>
    Dep.target = watcher
    obj.b.a
    Dep.target = <span class="hljs-literal">null</span>
    expect(watcher.deps.length).toBe(<span class="hljs-number">3</span>) <span class="hljs-comment">// obj.b + b + b.a</span>
    obj.b.a = <span class="hljs-number">3</span>
    expect(watcher.update.calls.count()).toBe(<span class="hljs-number">1</span>)
    watcher.deps = []
  });
</code></pre>
<p>obj.b&#x81EA;&#x5DF1;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x68C0;&#x67E5;&#x6539;&#x53D8;obj.b&#x662F;&#x5426;&#x88AB;&#x901A;&#x77E5;&#x6765;&#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x76D1;&#x6D4B;&#x662F;&#x5426;&#x652F;&#x6301;&#x3002;</p>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x5BF9;val&#x9012;&#x5F52;&#x5730;&#x8C03;&#x7528;observe&#x65B9;&#x6CD5;&#x3002;&#x56E0;&#x4E3A;&#x5982;&#x679C;val&#x4E0D;&#x662F;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;observe&#x65B9;&#x6CD5;&#x5C31;&#x4F1A;&#x8FD4;&#x56DE;&#x3002;&#x6240;&#x4EE5;&#x5F53;&#x6211;&#x4EEC;&#x7528;defineReactive&#x6765;&#x76D1;&#x63A7;&#x4E00;&#x5806;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x8C03;&#x7528;observe&#x65B9;&#x6CD5;&#x5E76;&#x628A;&#x8FD4;&#x56DE;&#x503C;&#x4FDD;&#x5B58;&#x5728;childOb&#x4E0A;&#x3002;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">defineReactive</span> (<span class="hljs-params">obj, key, val</span>) </span>{
  <span class="hljs-keyword">var</span> dep = <span class="hljs-keyword">new</span> Dep()
  <span class="hljs-keyword">var</span> childOb = observe(val) <span class="hljs-comment">// new</span>
  <span class="hljs-built_in">Object</span>.defineProperty(obj, key, {
    &#xB7;&#xB7;&#xB7;
  })
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x8981;&#x5B58;&#x4E0B;&#x5B50;observer&#x7684;&#x5F15;&#x7528;&#x7684;&#x539F;&#x56E0;&#x662F;&#xFF0C;&#x5F53;getter&#x88AB;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x5B50;&#x5BF9;&#x8C61;&#x4E0A;&#x91CD;&#x65B0;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x3002;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
get: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reactiveGetter</span> (<span class="hljs-params"></span>) </span>{
      <span class="hljs-keyword">var</span> value = val
      <span class="hljs-keyword">if</span> (Dep.target) {
        dep.depend()
        <span class="hljs-comment">// re-collect for childOb</span>
        <span class="hljs-keyword">if</span> (childOb) {
          childOb.dep.depend()
        }
      }
      <span class="hljs-keyword">return</span> value
    }
&#xB7;&#xB7;&#xB7;
</code></pre>
<p>&#x540C;&#x65F6;&#x5728;setter&#x88AB;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x4E5F;&#x8981;&#x91CD;&#x65B0;&#x76D1;&#x63A7;&#x5B50;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
set: <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reactiveSetter</span> (<span class="hljs-params">newVal</span>) </span>{
      <span class="hljs-keyword">var</span> value =  val
      <span class="hljs-keyword">if</span> (newVal === value || (newVal !== newVal &amp;&amp; value !== value)) {
        <span class="hljs-keyword">return</span>
      }
      val = newVal
      childOb = observe(newVal) <span class="hljs-comment">//new</span>
      dep.notify()
    }
&#xB7;&#xB7;&#xB7;
</code></pre>
<h2 id="24-&#x89C2;&#x6D4B;&#x8BBE;&#x7F6E;&#x6216;&#x5220;&#x9664;&#x6570;&#x636E;">2.4 &#x89C2;&#x6D4B;&#x8BBE;&#x7F6E;&#x6216;&#x5220;&#x9664;&#x6570;&#x636E;</h2>
<p>Vue&#x5728;&#x76D1;&#x63A7;&#x6570;&#x636E;&#x65B9;&#x9762;&#x6709;&#x4E00;&#x4E9B;&#x8B66;&#x793A;&#x3002;&#x56E0;&#x4E3A;Vue&#x5904;&#x7406;&#x6570;&#x636E;&#x53D8;&#x5316;&#x7684;&#x65B9;&#x5F0F;,&#x5B83;&#x4E0D;&#x80FD;&#x76D1;&#x63A7;&#x5230;&#x5C5E;&#x6027;&#x7684;&#x6DFB;&#x52A0;&#x548C;&#x5220;&#x9664;&#x3002;&#x6570;&#x636E;&#x53EA;&#x6709;&#x5728;getter&#x548C;setter&#x88AB;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x624D;&#x80FD;&#x88AB;&#x76D1;&#x63A7;&#x5230;&#xFF0C;&#x4F46;&#x8BBE;&#x7F6E;&#x6216;&#x5220;&#x9664;&#x6570;&#x636E;&#xFF0C;getter&#x548C;setter&#x5E76;&#x4E0D;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;&#x4F7F;&#x7528;Vue.set(object, key, value)&#x65B9;&#x6CD5;&#x7ED9;&#x5D4C;&#x5957;&#x7684;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x54CD;&#x5E94;&#x5C5E;&#x6027;&#xFF0C;&#x548C;Vue.delete(object, key, value)&#x6765;&#x5220;&#x9664;&#x54CD;&#x5E94;&#x5C5E;&#x6027;&#x662F;&#x53EF;&#x80FD;&#x7684;&#x3002;</p>
<p>&#x50CF;&#x4E4B;&#x524D;&#x4E00;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x6765;&#x5199;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;:</p>
<p>test/observer/observer.spec.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  Observer,
  observe,
  set <span class="hljs-keyword">as</span> setProp, <span class="hljs-comment">//new</span>
  del <span class="hljs-keyword">as</span> delProp  <span class="hljs-comment">//new</span>
}
<span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../../src/observer/index&quot;</span>
<span class="hljs-keyword">import</span> {
  hasOwn,
  isObject
}
<span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../util/index&apos;</span> <span class="hljs-comment">//new</span>

describe(<span class="hljs-string">&apos;Observer test&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// new test case</span>
  it(<span class="hljs-string">&apos;observing set/delete&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> obj1 = {
      a: <span class="hljs-number">1</span>
    }
    <span class="hljs-comment">// should notify set/delete data</span>
    <span class="hljs-keyword">const</span> ob1 = observe(obj1)
    <span class="hljs-keyword">const</span> dep1 = ob1.dep
    spyOn(dep1, <span class="hljs-string">&apos;notify&apos;</span>)
    setProp(obj1, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-number">2</span>)
    expect(obj1.b).toBe(<span class="hljs-number">2</span>)
    expect(dep1.notify.calls.count()).toBe(<span class="hljs-number">1</span>)
    delProp(obj1, <span class="hljs-string">&apos;a&apos;</span>)
    expect(hasOwn(obj1, <span class="hljs-string">&apos;a&apos;</span>)).toBe(<span class="hljs-literal">false</span>)
    expect(dep1.notify.calls.count()).toBe(<span class="hljs-number">2</span>)
    <span class="hljs-comment">// set existing key, should be a plain set and not</span>
    <span class="hljs-comment">// trigger own ob&apos;s notify</span>
    setProp(obj1, <span class="hljs-string">&apos;b&apos;</span>, <span class="hljs-number">3</span>)
    expect(obj1.b).toBe(<span class="hljs-number">3</span>)
    expect(dep1.notify.calls.count()).toBe(<span class="hljs-number">2</span>)
    <span class="hljs-comment">// should ignore deleting non-existing key</span>
    delProp(obj1, <span class="hljs-string">&apos;a&apos;</span>)
    expect(dep1.notify.calls.count()).toBe(<span class="hljs-number">3</span>)
  });
  &#xB7;&#xB7;&#xB7;
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x5728;Observer&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x91CC;&#x6DFB;&#x52A0;&#x4E86;&apos;&#x76D1;&#x63A7;&#x8BBE;&#x7F6E;/&#x5220;&#x9664;&apos;&#x7684;&#x65B0;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;:</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span> (<span class="hljs-params">obj, key, val</span>) </span>{
  <span class="hljs-keyword">if</span> (hasOwn(obj, key)) {
    obj[key] = val
    <span class="hljs-keyword">return</span>
  }
  <span class="hljs-keyword">const</span> ob = obj.__ob__
  <span class="hljs-keyword">if</span> (!ob) {
    obj[key] = val
    <span class="hljs-keyword">return</span>
  }
  defineReactive(ob.value, key, val)
  ob.dep.notify()
  <span class="hljs-keyword">return</span> val
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">del</span> (<span class="hljs-params">obj, key</span>) </span>{
  <span class="hljs-keyword">const</span> ob = obj.__ob__
  <span class="hljs-keyword">if</span> (!hasOwn(obj, key)) {
    <span class="hljs-keyword">return</span>
  }
  <span class="hljs-keyword">delete</span> obj[key]
  <span class="hljs-keyword">if</span> (!ob) {
    <span class="hljs-keyword">return</span>
  }
  ob.dep.notify()
}
</code></pre>
<p>set&#x65B9;&#x6CD5;&#x5148;&#x68C0;&#x6D4B;&#x5C5E;&#x6027;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#xFF0C;&#x8BBE;&#x7F6E;&#x65B0;&#x503C;&#x5E76;&#x8FD4;&#x56DE;&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x901A;&#x8FC7;obj.<strong>ob</strong>&#x68C0;&#x6D4B;&#x8BE5;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x54CD;&#x5E94;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x8FD4;&#x56DE;&#x3002;&#x5982;&#x679C;key&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5C31;&#x7528;defineReactive&#x521B;&#x5EFA;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x5E76;&#x8C03;&#x7528;ob.dep.notify()&#x901A;&#x77E5;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x503C;&#x5DF2;&#x7ECF;&#x6539;&#x53D8;&#x4E86;&#x3002;</p>
<p>delete&#x65B9;&#x6CD5;&#x5C31;&#x5982;&#x9884;&#x671F;&#x7684;&#x90A3;&#x6837;&#xFF0C;&#x901A;&#x8FC7;delete&#x64CD;&#x4F5C;&#x7B26;&#x5220;&#x9664;&#x4E86;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x3002;</p>
<h2 id="25-&#x76D1;&#x63A7;&#x6570;&#x7EC4;">2.5 &#x76D1;&#x63A7;&#x6570;&#x7EC4;</h2>
<p>&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4E0D;&#x80FD;&#x76D1;&#x63A7;&#x6570;&#x7EC4;&#x53D8;&#x5316;&#x3002;&#x56E0;&#x4E3A;&#x901A;&#x8FC7;&#x4E0B;&#x6807;&#x8BBF;&#x95EE;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x5E76;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1;getter&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x7684;getter/setter&#x65B9;&#x6CD5;&#x4E0D;&#x9002;&#x7528;&#x4E8E;&#x6570;&#x7EC4;&#x76D1;&#x63A7;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x76D1;&#x63A7;&#x6570;&#x7EC4;&#x53D8;&#x5316;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x52AB;&#x6301;&#x4E00;&#x4E9B;&#x6570;&#x7EC4;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982;Array.prototype.pop()&#x548C;Array.prototype.shift(),&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;&#x5728;&#x6700;&#x540E;&#x5B9E;&#x73B0;&#x7684;Vue.set API&#xFF0C;&#x6765;&#x66FF;&#x4EE3;&#x901A;&#x8FC7;&#x4E0B;&#x6807;&#x8BBE;&#x7F6E;&#x6570;&#x7EC4;&#x7684;&#x503C;&#x3002;
&#x4E0B;&#x9762;&#x662F;&#x201D;&#x76D1;&#x63A7;&#x6570;&#x7EC4;&#x53D8;&#x5316;&#x201C;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6570;&#x7EC4;API&#x6765;&#x6539;&#x53D8;&#x6570;&#x7EC4;&#xFF0C;&#x53D8;&#x5316;&#x5C06;&#x88AB;&#x76D1;&#x63A7;&#x5230;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x4E5F;&#x4F1A;&#x88AB;&#x76D1;&#x63A7;&#x5230;&#x3002;</p>
<p>test/observer/observer.spec.js</p>
<pre><code class="lang-js">describe(<span class="hljs-string">&apos;Observer test&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// new</span>
  it(<span class="hljs-string">&apos;observing array mutation&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> arr = []
    <span class="hljs-keyword">const</span> ob = observe(arr)
    <span class="hljs-keyword">const</span> dep = ob.dep
    spyOn(dep, <span class="hljs-string">&apos;notify&apos;</span>)
    <span class="hljs-keyword">const</span> objs = [{}, {}, {}]
    arr.push(objs[<span class="hljs-number">0</span>])
    arr.pop()
    arr.unshift(objs[<span class="hljs-number">1</span>])
    arr.shift()
    arr.splice(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, objs[<span class="hljs-number">2</span>])
    arr.sort()
    arr.reverse()
    expect(dep.notify.calls.count()).toBe(<span class="hljs-number">7</span>)
    <span class="hljs-comment">// inserted elements should be observed</span>
    objs.forEach(obj =&gt; {
      expect(obj.__ob__ <span class="hljs-keyword">instanceof</span> Observer).toBe(<span class="hljs-literal">true</span>)
    })
  });
  &#xB7;&#xB7;&#xB7;
}
</code></pre>
<p>&#x7B2C;&#x4E00;&#x6B65;&#x662F;&#x5728;Oberver&#x65B9;&#x6CD5;&#x91CC;&#x5904;&#x7406;&#x6570;&#x7EC4;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Observer</span>(<span class="hljs-params">value</span>) </span>{
  <span class="hljs-keyword">this</span>.value = value
  <span class="hljs-keyword">this</span>.dep = <span class="hljs-keyword">new</span> Dep()
  <span class="hljs-comment">//this.walk(value) //deleted</span>
  <span class="hljs-comment">// new</span>
  <span class="hljs-keyword">if</span>(<span class="hljs-built_in">Array</span>.isArray(value)){
    <span class="hljs-keyword">this</span>.observeArray(value)
  }<span class="hljs-keyword">else</span>{
    <span class="hljs-keyword">this</span>.walk(value)
  }
  def(value, <span class="hljs-string">&apos;__ob__&apos;</span>, <span class="hljs-keyword">this</span>)
}
</code></pre>
<p>observeArray&#x4EC5;&#x4EC5;&#x662F;&#x904D;&#x5386;&#x6570;&#x7EC4;&#xFF0C;&#x5BF9;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x8C03;&#x7528;oberve&#x65B9;&#x6CD5;&#x3002;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
Observer.prototype.observeArray = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">items</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, l = items.length; i &lt; l; i++) {
    observe(items[i])
  }
}
</code></pre>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4FEE;&#x6539;Array&#x539F;&#x578B;&#x94FE;&#x6765;&#x8986;&#x76D6;&#x539F;&#x6765;&#x7684;Array&#x65B9;&#x6CD5;</p>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5355;&#x4F8B;&#xFF0C;&#x5B83;&#x62E5;&#x6709;&#x6240;&#x6709;&#x88AB;&#x6539;&#x53D8;&#x4E86;&#x7684;&#x6570;&#x7EC4;&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E9B;&#x6570;&#x7EC4;&#x65B9;&#x6CD5;&#x4E3A;&#x4E86;&#x5904;&#x7406;&#x53D8;&#x5316;&#x68C0;&#x6D4B;&#xFF0C;&#x52A0;&#x4E0A;&#x4E86;&#x522B;&#x7684;&#x903B;&#x8F91;&#x3002;</p>
<p>src/observer/array.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { def } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../util/index&apos;</span>

<span class="hljs-keyword">const</span> arrayProto = <span class="hljs-built_in">Array</span>.prototype
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> arrayMethods = <span class="hljs-built_in">Object</span>.create(arrayProto)

<span class="hljs-comment">/**
 * Intercept mutating methods and emit events
 */</span>
;[
  <span class="hljs-string">&apos;push&apos;</span>,
  <span class="hljs-string">&apos;pop&apos;</span>,
  <span class="hljs-string">&apos;shift&apos;</span>,
  <span class="hljs-string">&apos;unshift&apos;</span>,
  <span class="hljs-string">&apos;splice&apos;</span>,
  <span class="hljs-string">&apos;sort&apos;</span>,
  <span class="hljs-string">&apos;reverse&apos;</span>
]
.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">method</span>) </span>{
  <span class="hljs-comment">// cache original method</span>
  <span class="hljs-keyword">const</span> original = arrayProto[method]
  def(arrayMethods, method, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mutator</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> i = <span class="hljs-built_in">arguments</span>.length
    <span class="hljs-keyword">const</span> args = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(i)
    <span class="hljs-keyword">while</span> (i--) {
      args[i] = <span class="hljs-built_in">arguments</span>[i]
    }
    <span class="hljs-keyword">const</span> result = original.apply(<span class="hljs-keyword">this</span>, args)
    <span class="hljs-keyword">const</span> ob = <span class="hljs-keyword">this</span>.__ob__
    <span class="hljs-keyword">let</span> inserted
    <span class="hljs-keyword">switch</span> (method) {
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;push&apos;</span>:
        inserted = args
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;unshift&apos;</span>:
        inserted = args
        <span class="hljs-keyword">break</span>
      <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;splice&apos;</span>:
        inserted = args.slice(<span class="hljs-number">2</span>)
        <span class="hljs-keyword">break</span>
    }
    <span class="hljs-keyword">if</span> (inserted) ob.observeArray(inserted)
    <span class="hljs-comment">// notify change</span>
    ob.dep.notify()
    <span class="hljs-keyword">return</span> result
  })
})
</code></pre>
<p>arrayMethods&#x5C31;&#x662F;&#x90A3;&#x4E2A;&#x62E5;&#x6709;&#x6240;&#x6709;&#x5DF2;&#x7ECF;&#x88AB;&#x6539;&#x53D8;&#x65B9;&#x6CD5;&#x7684;&#x5355;&#x4F8B;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6240;&#x6709;&#x65B9;&#x6CD5;</p>
<blockquote>
<p>[&apos;push&apos;,&apos;pop&apos;,&apos;shift&apos;,&apos;unshift&apos;,&apos;splice&apos;,&apos;sort&apos;,&apos;reverse&apos;]</p>
</blockquote>
<p>&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;mutator&#x65B9;&#x6CD5;&#x6765;&#x6539;&#x53D8;&#x539F;&#x6765;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5728;mutator&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x9996;&#x5148;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x53C2;&#x6570;&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x91CC;&#xFF0C;&#x7136;&#x540E;&#x5BF9;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x6570;&#x7EC4;&#x8C03;&#x7528;&#x6240;&#x6709;&#x7684;&#x539F;&#x6765;&#x6570;&#x7EC4;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4FDD;&#x5B58;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x5F53;&#x5BF9;&#x6570;&#x7EC4;&#x6DFB;&#x52A0;&#x65B0;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x5BF9;&#x65B0;&#x5143;&#x7D20;&#x6570;&#x7EC4;&#x8C03;&#x7528;observeArray&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x6700;&#x540E;&#x6211;&#x4EEC;&#x901A;&#x8FC7;ob.dep.notify()&#x6765;&#x901A;&#x77E5;&#x53D8;&#x5316;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x6B65;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x628A;&#x8FD9;&#x4E2A;&#x5355;&#x4F8B;&#x52A0;&#x5230;&#x539F;&#x578B;&#x94FE;&#x91CC;&#x9762;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x5F53;&#x524D;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;<strong>proto</strong>&#x4E0B;&#x6807;&#x7684;&#x8BDD;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x628A;&#x6570;&#x7EC4;&#x7684;&#x539F;&#x578B;&#x94FE;&#x76F4;&#x63A5;&#x6307;&#x5411;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684;&#x5355;&#x4F8B;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6D4F;&#x89C8;&#x5668;&#x4E0D;&#x652F;&#x6301;<strong>proto</strong>&#x7684;&#x8BDD;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x628A;arrayMethods&#x5355;&#x4F8B;&#x6DF7;&#x5165;&#x76D1;&#x63A7;&#x7684;&#x6570;&#x7EC4;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x5DE5;&#x5177;&#x51FD;&#x6570;&#xFF1A;</p>
<p>src/observer/index.js</p>
<pre><code class="lang-js"><span class="hljs-comment">// helpers</span>
<span class="hljs-comment">/**
 * Augment an target Object or Array by intercepting
 * the prototype chain using __proto__
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">protoAugment</span> (<span class="hljs-params">target, src</span>) </span>{
  target.__proto__ = src
}

<span class="hljs-comment">/**
 * Augment an target Object or Array by defining
 * properties.
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">copyAugment</span> (<span class="hljs-params">target, src, keys</span>) </span>{
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, l = keys.length; i &lt; l; i++) {
    <span class="hljs-keyword">var</span> key = keys[i]
    def(target, key, src[key])
  }
}
</code></pre>
<p>&#x5728;Observer&#x65B9;&#x6CD5;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x6839;&#x636E;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>proto</strong>,&#x6765;&#x51B3;&#x5B9A;&#x8C03;&#x7528;protoAugment&#x6216;copyAugment&#x3002;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x539F;&#x59CB;&#x6570;&#x7EC4;&#x589E;&#x5F3A;&#x529F;&#x80FD;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
  def,
  hasOwn,
  hasProto, <span class="hljs-comment">//new</span>
  isObject
}
<span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../util/index&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Observer</span>(<span class="hljs-params">value</span>) </span>{
  <span class="hljs-keyword">this</span>.value = value
  <span class="hljs-keyword">this</span>.dep = <span class="hljs-keyword">new</span> Dep()
  <span class="hljs-keyword">if</span>(<span class="hljs-built_in">Array</span>.isArray(value)){
    <span class="hljs-comment">//new</span>
    <span class="hljs-keyword">var</span> augment = hasProto
        ? protoAugment
        : copyAugment
      augment(value, arrayMethods, arrayKeys)
    <span class="hljs-keyword">this</span>.observeArray(value)
  }<span class="hljs-keyword">else</span>{
    <span class="hljs-keyword">this</span>.walk(value)
  }
  def(value, <span class="hljs-string">&apos;__ob__&apos;</span>, <span class="hljs-keyword">this</span>)
}
</code></pre>
<p>hasProto&#x7684;&#x5B9A;&#x4E49;&#x5F88;&#x7B80;&#x5355;</p>
<p>src/util/index.js</p>
<pre><code class="lang-js">&#xB7;&#xB7;&#xB7;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">var</span> hasProto = <span class="hljs-string">&apos;__proto__&apos;</span> <span class="hljs-keyword">in</span> {}
</code></pre>
<p>&#x73B0;&#x5728;&#x5E94;&#x8BE5;&#x80FD;&#x901A;&#x8FC7;&#x90A3;&#x4E2A;&#x201D;&#x76D1;&#x63A7;&#x6570;&#x7EC4;&#x53D8;&#x5316;&#x201C;&#x7684;&#x6D4B;&#x8BD5;&#x4E86;</p>
<h2 id="26-watcher">2.6 Watcher</h2>
<p>&#x6211;&#x4EEC;&#x5728;&#x4E4B;&#x524D;&#x6A21;&#x62DF;&#x4E86;Watcher:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> watcher = {
  deps: [],
  addDep (dep) {
    <span class="hljs-keyword">this</span>.deps.push(dep)
    dep.addSub(<span class="hljs-keyword">this</span>)
    },
    update: jasmine.createSpy()
}
</code></pre>
<p>&#x6240;&#x4EE5;&#x8FD9;&#x91CC;watcher&#x5728;&#x8FD9;&#x91CC;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x6709;deps&#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;deps&#x7528;&#x6765;&#x8BB0;&#x5F55;&#x8BE5;watcher&#x6240;&#x4EE5;&#x7684;&#x4F9D;&#x8D56;&#x3002;&#x5B83;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;addDep&#x65B9;&#x6CD5;&#x6765;&#x6DFB;&#x52A0;&#x4F9D;&#x8D56;&#x3002;&#x4E00;&#x4E2A;update&#x65B9;&#x6CD5;&#xFF0C;&#x5F53;&#x76D1;&#x63A7;&#x7684;&#x6570;&#x636E;&#x53D8;&#x5316;&#x65F6;&#x5019;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E0B;Watcher&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7B7E;&#x540D;:</p>
<pre><code class="lang-js"><span class="hljs-keyword">constructor</span> (
    vm: Component,
    expOrFn: string | Function,
    cb: Function,
    options?: Object
  )
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;Watcher&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E00;&#x4E2A;expOrFn&#x548C;&#x4E00;&#x4E2A;cb&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;expOrFn&#x662F;&#x4E00;&#x4E2A;&#x5728;&#x521D;&#x59CB;&#x5316;watcher&#x7684;&#x65F6;&#x5019;&#x5C31;&#x6267;&#x884C;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x65B9;&#x6CD5;&#x3002;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x662F;&#x5728;&#x5F53;watcher&#x9700;&#x8981;&#x6267;&#x884C;&#x56DE;&#x8C03;&#x7684;&#x65F6;&#x5019;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x5E94;&#x8BE5;&#x80FD;&#x63ED;&#x5F00;worker&#x7684;&#x795E;&#x79D8;&#x9762;&#x7EB1;&#x3002;</p>
<p>test/observer/watcher.spec.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../../src/instance/index&quot;</span>;
<span class="hljs-keyword">import</span> Watcher <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;../../src/observer/watcher&quot;</span>;

describe(<span class="hljs-string">&apos;Wathcer test&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  it(<span class="hljs-string">&apos;should call callback when simple data change&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue({
      data:{
        a:<span class="hljs-number">2</span>
      }
    })
    <span class="hljs-keyword">var</span> cb = jasmine.createSpy(<span class="hljs-string">&apos;callback&apos;</span>);
    <span class="hljs-keyword">var</span> watcher = <span class="hljs-keyword">new</span> Watcher(vm, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
      <span class="hljs-keyword">var</span> a = vm.a
    }, cb)
    vm.a = <span class="hljs-number">5</span>;
    expect(cb).toHaveBeenCalled();
  });
}
</code></pre>
<p>expOrFn&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x6240;&#x4EE5;vm&#x6570;&#x636E;&#x7684;&#x54CD;&#x5E94;&#x5F0F;getter&#x4F1A;&#x88AB;&#x8C03;&#x7528;(&#x8FD9;&#x91CC;&#x662F;vm.a&#x7684;getter).watcher&#x628A;&#x81EA;&#x5DF1;&#x8BBE;&#x7F6E;&#x4E3A;Dep&#x7684;target.&#x6240;&#x4EE5;vm.a&#x7684;dep&#x5C31;&#x4F1A;&#x628A;&#x8BE5;watcher&#x5B9E;&#x4F8B;&#x653E;&#x5230;&#x81EA;&#x5DF1;&#x7684;subs&#x6570;&#x7EC4;&#x91CC;&#x3002;watcher&#x4F1A;&#x628A;vm.a&#x7684;dep&#x63A8;&#x8FDB;&#x81EA;&#x5DF1;&#x7684;deps&#x6570;&#x7EC4;&#x3002;&#x5F53;vm.a&#x7684;setter&#x88AB;&#x8C03;&#x7528;&#x3002;vm.a&#x4E0B;&#xFF0C;dep&#x7684;subs&#x6570;&#x7EC4;&#x5C31;&#x4F1A;&#x88AB;&#x904D;&#x5386;&#x5E76;&#x4E14;subs&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;watcher&#x7684;update&#x65B9;&#x6CD5;&#x5C06;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x6700;&#x540E;watcher&#x7684;&#x56DE;&#x8C03;&#x5C06;&#x4F1A;&#x88AB;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0;watcher&#x7C7B;&#xFF1A;</p>
<p>src/observer/watcher.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> uid = <span class="hljs-number">0</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Watcher</span>(<span class="hljs-params">vm, expOrFn, cb, options</span>) </span>{
  options = options ? options : {}
  <span class="hljs-keyword">this</span>.vm = vm
  vm._watchers.push(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">this</span>.cb = cb
  <span class="hljs-keyword">this</span>.id = ++uid

  <span class="hljs-comment">// options</span>
  <span class="hljs-keyword">this</span>.deps = []
  <span class="hljs-keyword">this</span>.newDeps = []
  <span class="hljs-keyword">this</span>.depIds = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>()
  <span class="hljs-keyword">this</span>.newDepIds = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>()
  <span class="hljs-keyword">this</span>.getter = expOrFn
  <span class="hljs-keyword">this</span>.value = <span class="hljs-keyword">this</span>.get()
}
</code></pre>
<p>Watcher&#x7C7B;&#x4F1A;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E9B;&#x5C5E;&#x6027;&#x3002;&#x6BCF;&#x4E2A;Watcher&#x5B9E;&#x4F8B;&#x4F1A;&#x4FDD;&#x7559;&#x4E00;&#x4E2A;id&#x4EE5;&#x5907;&#x540E;&#x7528;&#x3002;&#x8FD9;&#x4E2A;id&#x901A;&#x8FC7;&apos;this.id = ++ uid&apos;&#x8BBE;&#x7F6E;&#x3002;this.deps&#x548C;this.newDeps&#x662F;&#x5B58;&#x653E;deps&#x5BF9;&#x8C61;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x7528;&#x6765;&#x7BA1;&#x7406;Deps.&#x6211;&#x4EEC;&#x540E;&#x9762;&#x5C06;&#x77E5;&#x9053;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x6570;&#x7EC4;&#x3002;this.depIds&#x548C;this.newDepIds&#x662F;&#x76F8;&#x5BF9;&#x5E94;&#x7684;id&#x96C6;&#x5408;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD9;&#x4E9B;&#x96C6;&#x5408;&#x5FEB;&#x901F;&#x67E5;&#x627E;&#x5BF9;&#x5E94;&#x7684;dep&#x5B9E;&#x4F8B;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3002;</p>
<p>src/observer/watcher.js</p>
<pre><code class="lang-js">Watcher.prototype.get = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  pushTarget(<span class="hljs-keyword">this</span>)
  <span class="hljs-keyword">var</span> value = <span class="hljs-keyword">this</span>.getter.call(<span class="hljs-keyword">this</span>.vm, <span class="hljs-keyword">this</span>.vm)
  popTarget()
  <span class="hljs-keyword">this</span>.cleanupDeps()
  <span class="hljs-keyword">return</span> value
}
</code></pre>
<p>Watcher.prototype.get &#x65B9;&#x6CD5;&#x9996;&#x5148;push&#x5F53;&#x524D;&#x7684;Watcher&#x5B9E;&#x4F8B;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../创建你自己的react/创建你自己的react.html" class="navigation navigation-prev " aria-label="Previous page: 创建你自己的 react">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../JSXWTH.html" class="navigation navigation-next " aria-label="Next page: JSX 什么鬼(一起来写一个 JSX 渲染器)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"创建你自己的 vuejs","level":"1.2.2","depth":2,"next":{"title":"JSX 什么鬼(一起来写一个 JSX 渲染器)","level":"1.2.3","depth":2,"path":"前端框架/JSXWTH.md","ref":"./前端框架/JSXWTH.md","articles":[]},"previous":{"title":"创建你自己的 react","level":"1.2.1","depth":2,"path":"前端框架/创建你自己的react/创建你自己的react.md","ref":"./前端框架/创建你自己的react/创建你自己的react.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"前端框架/创建你自己的vuejs/创建你自己的vuejs.md","mtime":"2021-03-16T02:48:38.143Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-16T03:43:03.824Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

